package cn.drh.charging.common.utils;


import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.LinkedHashMap;

/**
 * @ClassName JsonUtils
 * @Description TODO
 * @Author renhong~Deng
 * @Date 2025/5/28 15:13
 * @Version 1.0
 * 通过Jackson实现的json的对象转换工具
 */

public class JsonUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 对象转json
     */
    public static String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * json转对象
     */
    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将map转换为对象
     * @param object
     * @param clazz
     * @return
     * @param <T>
     */
    public static <T> T coverValue(Object object, Class<T> clazz) {
        return objectMapper.convertValue(object, clazz);
    }
}
